import org.junit.internal.runners.statements.RunAfters;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TraceThreadPoolExecutor extends ThreadPoolExecutor {
    public TraceThreadPoolExecutor(int i, int i1, long l, TimeUnit timeUnit, BlockingQueue<Runnable> blockingQueue) {
        super(i, i1, l, timeUnit, blockingQueue);
    }

    @Override
    public void execute(Runnable runnable) {
        super.execute(wrap(runnable, taskTrace()));
    }

    @Override
    public Future<?> submit(Runnable runnable) {
        return super.submit(wrap(runnable, taskTrace()));
    }

    private Exception taskTrace() {
        return  new Exception("Task stack trace");
    }

    private Runnable wrap(final Runnable task, final Exception ex) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    task.run();
                } catch (Exception e) {
                    ex.printStackTrace();
                    throw e;
                }
            }
        };
    }
}
